//
//  PasswordViewController.m
//  Daybook
//
//  Created by liutaifeng on 16/6/1.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "PasswordViewController.h"
#import "Common.h"
#import "CLLockVC.h"
#import "CoreLockConst.h"
@interface PasswordViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UISwitch *cancelSwitch;
@end

@implementation PasswordViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([CLLockVC hasPwd]) {
        _cancelSwitch.on = NO;
    }
    else {
        _cancelSwitch.on = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
}
- (void)createView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, MAIN_WIDTH, 44)];
    [self.view addSubview:_topView];
    //_topView.backgroundColor = [UIColor orangeColor];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(MAIN_WIDTH / 2 - 100, 0, 200, 44)];
    [_topView addSubview:_titleLabel];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = @"密码与安全";
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(10, 7, 30, 30);
    [_topView addSubview:_backButton];
    [_backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    //_backButton.backgroundColor = [UIColor redColor];
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, MAIN_WIDTH, 1)];
    [_topView addSubview:_lineView];
    _lineView.backgroundColor = LINE_COLOR;
    [_backButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MAIN_WIDTH, MAIN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 50;
    [_tableView registerClass:[PasswordTableViewCell class] forCellReuseIdentifier:NSStringFromClass([PasswordTableViewCell class])];
    _tableView.scrollEnabled = NO;
    self.cancelSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(MAIN_WIDTH - 60,_tableView.rowHeight * 2 + 9, 60, _tableView.rowHeight)];
    [_tableView addSubview:_cancelSwitch];
    [_cancelSwitch addTarget:self action:@selector(cancelSwitch:) forControlEvents:UIControlEventValueChanged];
    _cancelSwitch.on = NO;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PasswordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PasswordTableViewCell class])];
    if (0 == indexPath.row) {
        cell.titleLabel.text = @"设置密码";
    }
    else if (1 == indexPath.row) {
        cell.titleLabel.text = @"修改密码";
    }
    else {
        cell.titleLabel.text = @"取消密码";
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        [self set];
    }
    else if (1 == indexPath.row) {
        [self modify];
    }
    
}
- (void)set {
    BOOL hasPwd = [CLLockVC hasPwd];
    
    if(hasPwd){
        
        NSLog(@"已经设置过密码了，你可以验证或者修改密码");
    }else{
        
        [CLLockVC showSettingLockVCInVC:self successBlock:^(CLLockVC *lockVC, NSString *pwd) {
            
            NSLog(@"密码设置成功");
            [lockVC dismiss:1.0f];
        }];
    }
    
}
- (void)modify {
        BOOL hasPwd = [CLLockVC hasPwd];
    
        if(!hasPwd){
    
            NSLog(@"你还没有设置密码，请先设置密码");
    
        }else {
    
            [CLLockVC showModifyLockVCInVC:self successBlock:^(CLLockVC *lockVC, NSString *pwd) {
    
                [lockVC dismiss:.5f];
            }];
        }
}
- (void)cancelSwitch:(UISwitch *)cancelSwitch {
    if ([_cancelSwitch isOn] == 1) {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:nil forKey:CoreLockPWDKey];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"密码已解除" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
    } else {
        [self set];
    }

}
- (void)backButton:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
